package com.ruizhong.centercontroll.controller;


import com.ruizhong.centercontroll.entity.THall;
import com.ruizhong.centercontroll.entity.TScreen;
import com.ruizhong.centercontroll.service.TScreenService;
import com.ruizhong.centercontroll.util.ArtNetUtil;
import com.ruizhong.centercontroll.util.Result;
import com.ruizhong.centercontroll.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/screen/api")
@CrossOrigin
public class TScreenController {

    @Autowired
    private TScreenService tScreenService;


    @PostMapping("/submitNewScreen")
    public Result submitNewScreen(@RequestBody TScreen tScreen) {
        tScreenService.submitNewScreen(tScreen);
        return new Result(ResultCode.success, null, null);
    }

    @GetMapping("/getAllScreenList")
    public Result getAllScreenList() {
        Map<String, List<TScreen>> resMap = tScreenService.getAllScreenList();
        return new Result(ResultCode.success, null, resMap);
    }

    @GetMapping("/getScreenDetailById/{id}")
    public Result getScreenDetailById(@PathVariable(value = "id") Integer id) {
        TScreen screenDetail = tScreenService.getScreenDetailById(id);
        return new Result(ResultCode.success, null, screenDetail);
    }

    @PostMapping("/updateScreenInfo")
    public Result updateScreenInfo(@RequestBody TScreen tScreen) {
        tScreenService.updateScreenInfo(tScreen);
        return new Result(ResultCode.success, null, null);
    }

    @DeleteMapping("/deleteScreenById/{id}")
    public Result deleteScreenById(@PathVariable(value = "id") Integer id) {
        tScreenService.deleteScreenById(id);
        return new Result(ResultCode.success, null, null);
    }

    @GetMapping("/getAllHall")
    public Result getAllHall() {
        List<THall> hallArr = tScreenService.getAllHall();
        return new Result(ResultCode.success, null, hallArr);
    }

    @GetMapping("/getScreenListByHallName/{hallName}")
    public Result getScreenListByHallName(@PathVariable(value = "hallName") String hallName) {
        List<TScreen> screenList = tScreenService.getScreenListByHallName(hallName);
        return new Result(ResultCode.success, null, screenList);
    }

    @GetMapping("/jumpByCueId/{sqId}/{cueId}/{forAll}/{ip}")
    public Result jumpByCueId(@PathVariable(value = "cueId") Integer cueId,@PathVariable(value = "sqId") Integer sqId,@PathVariable(value = "forAll")Boolean forAll,@PathVariable(value = "ip")String ip) {
        tScreenService.jumpByCueId(sqId,cueId,forAll,ip);
        return new Result(ResultCode.success, null, null);
    }

    @GetMapping("/changeMode/{sqId}/{mode}/{forAll}/{ip}")
    public Result changeMode(@PathVariable(value = "mode") String mode,@PathVariable(value = "sqId") Integer sqId,@PathVariable(value = "forAll")Boolean forAll,@PathVariable(value = "ip")String ip){
        tScreenService.changeMode(sqId,mode,forAll,ip);
        return new Result(ResultCode.success,null,null);
    }

    @GetMapping("/nextLastCue/{sqId}/{isNext}/{forAll}/{ip}")
    public Result nextLastCue(@PathVariable(value = "isNext") Integer isNext,@PathVariable(value = "sqId") Integer sqId,@PathVariable(value = "forAll")Boolean forAll,@PathVariable(value = "ip")String ip){
        tScreenService.nextLastCue(sqId,isNext,forAll,ip);
        return new Result(ResultCode.success,null,null);
    }

    @GetMapping("/isFastForward/{sqId}/{isNext}/{forAll}/{ip}")
    public Result isFastForward(@PathVariable(value = "isNext") Integer isNext,@PathVariable(value = "sqId") Integer sqId,@PathVariable(value = "forAll")Boolean forAll,@PathVariable(value = "ip")String ip){
        tScreenService.isFastForward(sqId,isNext,forAll,ip);
        return new Result(ResultCode.success,null,null);
    }

    @PostMapping("/testLight")
    public Result testLight(@RequestBody Map conditionMap) throws Exception {
        String channelMapValue = conditionMap.get("channelMapValue").toString();
        String artnetip = conditionMap.get("ARTNETIP").toString();
        String area = conditionMap.get("AREA").toString();
        ArtNetUtil controller = new ArtNetUtil();
        String[] channelGroup = channelMapValue.split("#");
        for (String channelAndValue : channelGroup) {
            String[] arr = channelAndValue.split(">");
            int channel = Integer.parseInt(arr[0]);
            int value = Integer.parseInt(arr[1]);
            controller.setChannel(channel, value);
        }
        controller.sendDMX(artnetip, Integer.parseInt(area));
        return new Result(ResultCode.success,"成功",null);
    }

    @PostMapping("/changeOrder")
    public Result changeOrder(@RequestBody Map conditionMap){
       
        tScreenService.changeOrder(conditionMap);
        return new Result(ResultCode.success,null,null);
    }
    @PostMapping("/changeHallOrder")
    public Result changeHallOrder(@RequestBody Map conditionMap){
        tScreenService.changeHallOrder(conditionMap);
        return new Result(ResultCode.success,null,null);
    }

    @PostMapping("/test")
    public Result text(@RequestBody Map<String,String> conditionMap){
        System.out.println(conditionMap);
        return new Result(ResultCode.success,null,null);
    }
}
